package spc.webos.endpoint;

import org.apache.log4j.Logger;

public abstract class AbstractClusterEndpoint implements Endpoint {
	protected int retryInterval;
	protected int retryTimes = -1;
	protected Logger log = Logger.getLogger(super.getClass());

	public void init() throws Exception {
	}

	public void setLocation(String location) throws Exception {
		throw new RuntimeException("No method!!!");
	}

	protected boolean execute(Endpoint endpoint, Executable exe, int failCount)
			throws Exception {
		try {
			endpoint.execute(exe);
			return true;
		} catch (Exception e) {
			if (this.log.isInfoEnabled())
				this.log.info("cnnSnd: " + exe.cnnSnd + ", query:" + exe.query
						+ ", failCount:" + failCount + ", retryTimes:"
						+ this.retryTimes + ", retryInterval:"
						+ this.retryInterval + ", e:" + e);
			if ((!isClusterRetry(exe, e)) || (failCount >= this.retryTimes))
				throw e;
		}
		if (this.retryInterval > 0) {
			Thread.sleep(this.retryInterval);
		}
		return false;
	}

	protected boolean isClusterRetry(Executable exe, Exception e) {
		return (exe.query) || (!exe.cnnSnd);
	}

	public Endpoint clone() throws CloneNotSupportedException {
		throw new CloneNotSupportedException();
	}

	public int getRetryInterval() {
		return this.retryInterval;
	}

	public void setRetryInterval(int retryInterval) {
		this.retryInterval = retryInterval;
	}

	public void setRetryTimes(int retryTimes) {
		this.retryTimes = retryTimes;
	}

	public int getRetryTimes() {
		return this.retryTimes;
	}
}
